package com.wodwl.dao;

import java.io.IOException;

import java.io.Reader;

import java.util.List;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.wodwl.entity.Blog;

public class BlogDao {

	public static enum Mode {

		INSERT, UPDATE, DELETE

	}
	public void update(Object arg, Mode mode) {

		SqlMapClient smc = null;

		try {

			smc = getSqlMapClient();
			smc.startTransaction();

			switch (mode) {

			case INSERT:
				smc.insert("blog.insertBlog", arg);
				break;

			case UPDATE:
				smc.update("blog.updateBlogByBid", arg);
				break;

			case DELETE:
				smc.delete("blog.deleteBlog", arg);
				break;

			}

			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	
	
	public void delCategory(int cid){
		SqlMapClient smc = null;

		try {

			smc = getSqlMapClient();
			smc.startTransaction();
			smc.update("blog.delCategory", cid);
			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	public List<Blog> getBlogsByUsername(String username) {
		SqlMapClient smc = null;
		List<Blog> blogs=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			blogs =(List<Blog>) smc.queryForList("blog.getBlogsByUsername", username);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return blogs;

	}
	public List<Blog> getAllBlogs() {
		SqlMapClient smc = null;
		List<Blog> Blogs=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Blogs =(List<Blog>) smc.queryForList("blog.getAllBlogs", null);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return Blogs;

	}

	public Blog getBlogByBid(int id) {
		Blog Blog = null;
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Blog = (Blog) smc.queryForObject("blog.getBlogByBid", id);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);

		}
		return Blog;

	}

	// //
	// ////////////////////////////////////////////////////////////////////////

	private SqlMapClient getSqlMapClient() throws IOException {

		// 初始化ibatis, 获得一个SqlMapClient对象

		String resource = "ibatis/SqlMapConfig.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		return SqlMapClientBuilder.buildSqlMapClient(reader);

	}

	private void endTransaction(SqlMapClient smc) {

		if (smc == null) {
			return;
		}
		try {
			smc.endTransaction();
		} catch (Exception e2) {
			e2.printStackTrace();
		}

	}

	public static void main(String[] args) {
		BlogDao dao = new BlogDao();
		Blog blog=new Blog();
		
		/*
		 //增
		blog.setTitle("insert");
		blog.setContent("hello world");
		dao.update(blog, Mode.INSERT);
		*/
		
		//查
		//blog=dao.getBlogByBid(1);
		
		/*
		//改
		blog.setBid(1);
		blog.setTitle("update");
		dao.update(blog, Mode.UPDATE);
		*/
		
		//删
		//dao.update(1, Mode.DELETE);
		
		//List list=dao.getAllBlogs();
		 List<Blog> list=dao.getBlogsByUsername("aaa");
		System.out.println(list.size());
	}

}